package mp1.Controller;


import mp1.entity.Product;
import mp1.exception.ProductAdminException;
import mp1.mapper.ProductMapper;
import mp1.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("products")
public class ProductController {
    @Autowired
    ProductService productService;


    @GetMapping("/{id}")
    public ResponseEntity<Product> getProduct(@PathVariable long id){
        Product result = productService.findProById(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }


    @GetMapping("/{cate}")
    public ResponseEntity<List<Product>> getProductByCategory(@PathVariable String cate){
        List<Product> result = productService.findProByCategory(cate);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("/quantity")
    public ResponseEntity<List<Product>> getProductByQuantity(@RequestParam(value = "min") double min_q,@RequestParam(value = "max") double max_q){
        List<Product> result = productService.findProByQuantity(min_q,max_q);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("/price")
    public ResponseEntity<List<Product>> getProductByPrice(@RequestParam(value = "min") double pri_min,@RequestParam(value = "max") double pri_max){
        List<Product> result = productService.findProByQuantity(pri_min,pri_max);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("/{supplierName}")
    public ResponseEntity<List<Product>> getProductBySupplierName(@PathVariable String supplierName){
        List<Product> result = productService.findProBySupplier(supplierName);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }



    @DeleteMapping("/deleteProduct")
    public void deleteProduct(@PathVariable long id) throws ProductAdminException {
        productService.deleteProById(id);
    }


    @PostMapping("/addProduct")
    public Product addProduct(@RequestBody Product product) throws ProductAdminException {
        return productService.addPro(product);
    }


    @PutMapping("updateProduct/{id}")
    public void updateProduct(@RequestBody Product product) throws ProductAdminException {
        productService.updatePro(product);
    }





}
